<?php 

namespace Admin\Controller;
use Think\Controller;

/**
 * 培训商城控制器
 * @author 艾逗笔<381361943@qq.com>
 */
class ShopController extends BaseController {
	/**
	 * 培训商城列表页
	 * @author 艾逗笔<381361943@qq.com>
	 */
	public function index()
	{
		$shop = M('shop'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $shop
		->join("dc_type on dc_shop.type = dc_type.id")
		->order('dc_shop.id desc')
		->page($_GET['p'].',10')
		->field('dc_shop.id,dc_shop.title,dc_shop.picture,dc_shop.content,dc_shop.type,dc_type.name')
		->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $shop->join("dc_type on dc_shop.type = dc_type.id")->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
	    $Page->setConfig('prev', '上一页');
	    $Page->setConfig('next', '下一页');
	    $Page->setConfig('last', '末页');
	    $Page->setConfig('first', '首页');
	    $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
	    $Page->lastSuffix = false;//最后一页不显示为总页数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('count',$count );
		$this->display();
	}
	/**
	 * 培训商城列表添加
	 * @author 艾逗笔<381361943@qq.com>
	 */
	public function listadd()
	{
		if(IS_GET)
		{
			//获得分类
			$type = M('type')->select();
			$this->assign('type',$type);
			$this->display();
		}
		else
		{
			$data = I('post.');
			unset($data['__hash__']);
			$data['picture'] = $data['url'];
			$count=strpos($data['picture'],get_domain($data['picture']));
			$data['picture']=substr_replace($data['picture'],"",$count,strlen(get_domain($data['picture'])));
			unset($data['url']);
			$data['video'] = $data['videoUrl'];
			unset($data['videoUrl']);
            $data['time'] =time();
			M('shop')->add($data);
			$this->success('添加成功',U('index'));
		}
	}
	/**
	 * 培训商城列表编辑
	 * @author 艾逗笔<381361943@qq.com>
	 */
	public function listsave()
	{
		if(IS_GET)
		{
			$id = I('id',0);
			$id == 0?$this->error('参数错误,id不能为0'):1;
		  	$type = M('type')->select();
		  	$type == ''?$this->error('数据不存在'):1;
		  	$list = M('shop')->find($id);
		  	$list == ''?$this->error('数据不存在'):1;
		  	$this->assign('list',$list);
		  	$this->assign('type',$type);
		  	$this->display(); 
		}
		else
		{
			$data = I('post.');
			$id = $data['id'];
			unset($data['id']);
			unset($data['__hash__']);
            $count=strpos($data['url'],get_domain($data['url']));
            $data['picture']=substr_replace($data['url'],"",$count,strlen(get_domain($data['url'])));
			M('shop')->where(array('id'=>$id))->save($data);
			$this->success('修改成功',U('index'));
		}
	}
	/**
	 * 培训商城列表删除
	 * @author 艾逗笔<381361943@qq.com>
	 */
	public function listdelete()
	{
		$id = I('id','');
		$id == ''?$this->error():1;
		M('shop')->delete($id);
		$this->success('删除成功');
	}
	/**
	 * 培训商城分类
	 * @author 艾逗笔<765532665@qq.com>
	 */
	public function type()
	{
		$type = M('type'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		$list = $type->order('id desc')->page($_GET['p'].',10')->select();
		$this->assign('list',$list);// 赋值数据集
		$count      = $type->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header', '<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
	    $Page->setConfig('prev', '上一页');
	    $Page->setConfig('next', '下一页');
	    $Page->setConfig('last', '末页');
	    $Page->setConfig('first', '首页');
	    $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
	    $Page->lastSuffix = false;//最后一页不显示为总页数
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('count',$count );
		$this->display();
	}
	/**
	 * 培训商城增加分类
	 * @author 艾逗笔<765532665@qq.com>
	 */
	public function add()
	{
		if(IS_GET)
		{
			$this->display();
		}
		else
		{
			$data = I('post.');
			unset($data['__hash__']);
			$find = M('type')->where(array('name'=>$data['name']))->find();
			if($find)
				$this->error('该分类已经存在');
			M('type')->add($data);
			$this->success('添加成功',U('type'));
		}	
	}
	/**
	 * 培训商城修改分类
	 * @author 艾逗笔<765532665@qq.com>
	 */
	public function save()
	{
		if(IS_GET)
		{
			$id = I('id',0);
			$id == 0?$this->error('参数错误,id不能为0'):1;
		  	$list = M('type')->where(array('id'=>$id))->find();
		  	$list == ''?$this->error('数据不存在'):1;
		  	$this->assign('list',$list);
		  	$this->display(); 
		}
		else
		{
			$id = I('post.id',0);
			$id == 0?$this->error('参数无效'):1;
			$data = I('post.');
			unset($data['__hash__']);
			unset($data['id']);
			M('type')->where(array('id'=>$id))->save($data);
			$this->success('修改成功',U('type'));
		}	
	}
	/**
	 * 培训商城删除分类
	 * @author 艾逗笔<765532665@qq.com>
	 */
	public function delete()
	{
		$id = I('id',0);
		$id == 0?$this->error('参数无效'):1;
		M('type')->delete($id);
		$this->success('删除成功',U('type'));
	}
}